package com.zhenmyan.secdeal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenmyan.secdeal.Enum.ResponseEnum;
import com.zhenmyan.secdeal.exception.GlobalException;
import com.zhenmyan.secdeal.mapper.OrderMapper;
import com.zhenmyan.secdeal.pojo.Order;
import com.zhenmyan.secdeal.pojo.SecDealGoods;
import com.zhenmyan.secdeal.pojo.SecDealOrder;
import com.zhenmyan.secdeal.pojo.User;
import com.zhenmyan.secdeal.service.GoodsService;
import com.zhenmyan.secdeal.service.OrderService;
import com.zhenmyan.secdeal.service.SecDealGoodsService;
import com.zhenmyan.secdeal.service.SecDealOrderService;
import com.zhenmyan.secdeal.utils.MD5Util;
import com.zhenmyan.secdeal.utils.UUIDUtil;
import com.zhenmyan.secdeal.vo.GoodsVo;
import com.zhenmyan.secdeal.vo.OrderDetailVo;
import com.zhenmyan.secdeal.vo.Response;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * description:
 *
 * @author 颜真明
 * @date 2023/5/11  10:17
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private SecDealGoodsService secDealGoodsService;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private SecDealOrderService secDealOrderService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private GoodsService goodsService;

    @Override
    @Transactional
    public Order secDeal(User user, GoodsVo goodsVo) {

        SecDealGoods secDealGoods = secDealGoodsService
                .getOne(new QueryWrapper<SecDealGoods>().eq("goods_id", goodsVo.getId()));
        secDealGoods.setStockCount(secDealGoods.getStockCount() - 1);
        secDealGoodsService.updateById(secDealGoods);
        if (secDealGoods.getStockCount() < 1) {
            redisTemplate.opsForValue().set("isStockEmpty"+goodsVo.getId(), "0");
            return null;
        }
        //生成订单
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goodsVo.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsCount(1);
        order.setGoodsPrice(secDealGoods.getSecdealPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderMapper.insert(order);

        //生成秒杀订单
        SecDealOrder secDealOrder = new SecDealOrder();
        secDealOrder.setUserId(user.getId());
        secDealOrder.setOrderId(order.getId());
        secDealOrder.setGoodsId(goodsVo.getId());
        secDealOrderService.save(secDealOrder);

        return order;

    }

    @Override
    public OrderDetailVo detail(Long orderId) {
        if (null == orderId) {
            throw new GlobalException(ResponseEnum.ORDER_NOT_EXIST);
        }
        Order order = orderMapper.selectById(orderId);
        GoodsVo goodsVo = goodsService.getGoodsVoById(order.getGoodsId());
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setOrder(order);
        orderDetailVo.setGoodsVo(goodsVo);
        return orderDetailVo;
    }

    @Override
    public String createPath(User user,Long goodsId) {
        String str = MD5Util.md5(UUIDUtil.uuid() + "123456");
        redisTemplate.opsForValue().set("secDealPath:" + user.getId() + ":" + goodsId, str, 1, TimeUnit.MINUTES);
        return str;
    }

    @Override
    public boolean checkPath(User user,Long goodsId,String path) {
        if (user == null || goodsId < 0 || StringUtils.isEmpty(path)) {
            return false;
        }
        String redisPath = (String) redisTemplate.opsForValue().get("secDealPath:" + user.getId() + ":" + goodsId);
        return path.equals(redisPath);
    }

}
